package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : jiang
 * @date : 2023/10/14 17:22
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //分页查询
        Page<DishVO> dishPage = dishMapper.page(dishPageQueryDTO);
        //封装数据返回
        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(DishDTO dishDTO) {
        //创建菜品对象
        Dish dish = new Dish();
        //拷贝数据
        BeanUtils.copyProperties(dishDTO,dish);
        //操作菜品表 修改数据
        dishMapper.updateDish(dish);
        //删除菜品对应的口味
        dishFlavorMapper.delete(Arrays.asList(dish.getId()));
        //获取修改后的菜品口味
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        //判断菜品口味是否为空
        if (flavorList != null && flavorList.size() > 0){
            flavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //将口味数据添加到数据库
            dishFlavorMapper.addFlavor(flavorList);
        }
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDish(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            //判断菜品是否被套餐关联
            Dish dish = dishMapper.gatById(ids.get(i));
            //有关联 不能删除
            Integer count = setmealDishMapper.countByDishId(dish.getId());
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //判断菜品是否是起售状态
            Integer status = dish.getStatus();
            if (status.equals(StatusConstant.ENABLE)) {
                //起售状态 不可删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //没关联 是停售状态 可以删除
        dishMapper.delete(ids);
        //删除菜品后删除口味表中对应的口味
        dishFlavorMapper.delete(ids);
    }

    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDish(DishDTO dishDTO) {
        //创建菜品对象
        Dish dish = new Dish();
        //拷贝数据
        BeanUtils.copyProperties(dishDTO,dish);
        //添加到数据库
        dishMapper.addDish(dish);
        //构造菜品口味列表 保存数据到口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断口味是否为空
        if (flavors != null && flavors.size() > 0){
            //口味有值 补充数据
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            //添加到口味表中
            dishFlavorMapper.addFlavor(flavors);
        }

    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {
        //获取菜品数据
        Dish dish = dishMapper.gatById(id);
        //获取菜品对应的口味数据
        List<DishFlavor> dishFlavor = dishFlavorMapper.getById(id);
        //组装数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        //返回数据
        return dishVO;
    }

    /**
     * 修改菜品状态
     * @param status
     */
    @Override
    public void updateDishStatus(Integer status,Long id) {
        //构建dish对象
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.updateDish(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        //只展示起售的菜品
        List<Dish> dishes = dishMapper.getDishByCategoryId(categoryId,StatusConstant.ENABLE);
        return dishes;
    }

    /**
     * 根据分类id查询菜品及其口味
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> getDishByCategoryId(Long categoryId) {
        //只展示起售的菜品
        List<Dish> dishes = dishMapper.getDishByCategoryId(categoryId, StatusConstant.ENABLE);
        //创建一个dishVo集合
        ArrayList<DishVO> list = new ArrayList<>();
        //遍历查询到的菜品，获取菜品口味
        for (Dish dish : dishes) {
            //创建一个dishVO对象
            DishVO dishVO = new DishVO();
            //拷贝数据
            BeanUtils.copyProperties(dish, dishVO);
            //获取菜品口味信息 并赋值给dishVO
            dishVO.setFlavors(dishFlavorMapper.getById(dish.getId()));
            //收集dishVO到list集合中
            list.add(dishVO);
        }
        return list;
    }
}
